@let schema = dynamicSchema();
@if (schema) {
  <div
    [formGroup]="dynamicForm()"
    [class.indent]="schema?.indent"
  >
    @if (!(isHidden$ | async)) {
      @switch (schema.type) {
        @case (DynamicFormSchemaType.Cron) {
          <ix-scheduler
            [formControlName]="schema.controlName"
            [label]="schema.title ? (schema.title | translate) : ''"
            [tooltip]="schema.tooltip ? (schema.tooltip | translate) : ''"
            [required]="schema.required || false"
          ></ix-scheduler>
        }

        @case (DynamicFormSchemaType.Dict) {
          @let dictSchema = asDictSchema(schema);
          @if (dictSchema.title) {
            <div class="label">
              <div class="label-wrapper">
                {{ dictSchema.title }}
              </div>

              @if (dictSchema.tooltip) {
                <div class="label-tooltip">
                  <ix-tooltip
                    class="tooltip"
                    [header]="dictSchema.title"
                    [message]="dictSchema.tooltip"
                  ></ix-tooltip>
                </div>
              }
            </div>
          }

          @for (attr of dictSchema.attrs; track attr) {
            <ix-dynamic-form-item
              [id]="attr.controlName"
              [dynamicSchema]="attr"
              [dynamicForm]="dynamicForm()?.controls[dictSchema.controlName] | cast"
              [isEditMode]="isEditMode()"
              (addListItem)="addControlNext($event)"
              (deleteListItem)="removeControlNext($event)"
            ></ix-dynamic-form-item>
          }
        }

        @case (DynamicFormSchemaType.Text) {
          @let textSchema = asTextSchema(schema);
          <ix-code-editor
            [language]="textSchema.language"
            [label]="textSchema.title ? (textSchema.title | translate) : ''"
            [formControlName]="textSchema.controlName"
            [tooltip]="textSchema.tooltip ? (textSchema.tooltip | translate) : ''"
            [placeholder]="textSchema.placeholder ? (textSchema.placeholder | translate) : ''"
            [required]="textSchema.required || false"
          ></ix-code-editor>
        }

        @case (DynamicFormSchemaType.List) {
          @let listSchema = asListSchema(schema);
          <ix-list
            [label]="listSchema.title ? (listSchema.title | translate) : ''"
            [empty]="getFormArray.controls.length === 0"
            [formArrayName]="listSchema.controlName"
            [default]="listSchema.default"
            [tooltip]="listSchema.tooltip ? (listSchema.tooltip | translate) : ''"
            [itemsSchema]="listSchema.itemsSchema"
            [required]="listSchema.required || false"
            [isEditMode]="isEditMode()"
            [canAdd]="!isAllListControlsDisabled"
            [formArray]="getFormArray"
            (add)="addControl($event)"
          >
            @for (element of getFormArray.controls; track element; let i = $index) {
              <ix-list-item
                [label]="listSchema.title ? (listSchema.title | translate) : ''"
                [canDelete]="!isAllListControlsDisabled"
                (delete)="removeControl(i)"
              >
                @for (item of listSchema.items; track item) {
                  <ix-dynamic-form-item
                    [dynamicSchema]="item"
                    [dynamicForm]="element | cast"
                    [isEditMode]="isEditMode()"
                    (addListItem)="addControlNext($event)"
                    (deleteListItem)="removeControlNext($event)"
                  ></ix-dynamic-form-item>
                }
              </ix-list-item>
            }
            <ix-errors
              [control]="dynamicForm()"
              [label]="listSchema.title ? (listSchema.title | translate) : ''"
            ></ix-errors>
          </ix-list>
        }

        @case (DynamicFormSchemaType.Input) {
          @let inputSchema = asInputSchema(schema);
          <ix-input
            [formControlName]="inputSchema.controlName"
            [label]="inputSchema.title ? (inputSchema.title | translate) : ''"
            [placeholder]="inputSchema.placeholder ? (inputSchema.placeholder | translate) : ''"
            [tooltip]="inputSchema.tooltip ? (inputSchema.tooltip | translate) : ''"
            [required]="inputSchema.required || false"
            [type]="inputSchema.inputType || 'text'"
          ></ix-input>
        }

        @case (DynamicFormSchemaType.Uri) {
          @let uriSchema = asUriSchema(schema);
          <ix-input
            [formControlName]="uriSchema.controlName"
            [label]="uriSchema.title ? (uriSchema.title | translate) : ''"
            [placeholder]="uriSchema.placeholder ? (uriSchema.placeholder | translate) : ''"
            [tooltip]="uriSchema.tooltip ? (uriSchema.tooltip | translate) : ''"
            [required]="uriSchema.required || false"
            [type]="uriSchema.inputType || 'text'"
          ></ix-input>
        }

        @case (DynamicFormSchemaType.Select) {
          @let selectSchema = asSelectSchema(schema);
          <ix-select
            [formControlName]="selectSchema.controlName"
            [label]="selectSchema.title ? (selectSchema.title | translate) : ''"
            [options]="selectSchema.options"
            [tooltip]="selectSchema.tooltip ? (selectSchema.tooltip | translate) : ''"
            [required]="selectSchema.required || false"
            [hideEmpty]="selectSchema.hideEmpty || false"
          ></ix-select>
        }

        @case (DynamicFormSchemaType.Enum) {
          @let enumSchema = asEnumSchema(schema);
          <ix-combobox
            [formControlName]="enumSchema.controlName"
            [label]="enumSchema.title ? (enumSchema.title | translate) : ''"
            [provider]="getEnumTypeProvider(enumSchema.options)"
            [tooltip]="enumSchema.tooltip ? (enumSchema.tooltip | translate) : ''"
            [required]="enumSchema.required || false"
          ></ix-combobox>
        }

        @case (DynamicFormSchemaType.Explorer) {
          @let explorerSchema = asExplorerSchema(schema);
          <ix-explorer
            [formControlName]="explorerSchema.controlName"
            [label]="explorerSchema.title ? (explorerSchema.title | translate) : ''"
            [tooltip]="explorerSchema.tooltip ? (explorerSchema.tooltip | translate) : ''"
            [required]="explorerSchema.required || false"
            [canCreateDataset]="!isEditMode()"
            [nodeProvider]="explorerSchema.nodeProvider"
          ></ix-explorer>
        }

        @case (DynamicFormSchemaType.Checkbox) {
          <ix-checkbox
            [formControlName]="schema.controlName"
            [label]="schema.title ? (schema.title | translate) : ''"
            [tooltip]="schema.tooltip ? (schema.tooltip | translate) : ''"
            [required]="schema.required || false"
          ></ix-checkbox>
        }

        @case (DynamicFormSchemaType.Ipaddr) {
          <ix-ip-input-with-netmask
            [formControlName]="schema.controlName"
            [label]="schema.title ? (schema.title | translate) : ''"
            [tooltip]="schema.tooltip ? (schema.tooltip | translate) : ''"
            [required]="schema.required || false"
          ></ix-ip-input-with-netmask>
        }
      }
    }
  </div>
}
